<?php

/**
 * Contact form.
 *
 * @package    tfe
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ContactForm extends BaseContactForm {
    public function configure() {
        unset($this['created_at'], $this['updated_at']);


        if($this->getObject()->getDeletedAt() == "") {
            $this->widgetSchema['deleted_at'] = new sfWidgetFormInputHidden();
        }else {
            $this->widgetSchema['deleted_at'] = new sfWidgetFormDate();
        }


        $years = array();
        for($i=2015;$i>1899;$i--) {
            $years["$i"] = $i;
        }
        $this->widgetSchema['date_de_naissance'] = new sfWidgetFormI18nDate(array('culture'=>'fr', 'years'=>$years ));

        $this->widgetSchema['newsletters_list'] = new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Newsletter', 'order_by' => array('libelle','asc')));
        $this->widgetSchema['newsletters_list']->setOption('renderer_class', 'sfWidgetFormSelectDoubleList');
        $this->widgetSchema['newsletters_list']->setOption('renderer_options', array(
                'label_associated' => 'Sélectionnés',
                'label_unassociated' => 'Non sélectionnés'
        ));

        $this->widgetSchema['groupes_list'] = new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Groupe', 'order_by' => array('libelle','asc')));
        $this->widgetSchema['groupes_list']->setOption('renderer_class', 'sfWidgetFormSelectDoubleList');
        $this->widgetSchema['groupes_list']->setOption('renderer_options', array(
                'label_associated' => 'Sélectionnés',
                'label_unassociated' => 'Non sélectionnés'
        ));

        $this->embedRelation('NumerosDeTelephone');
        $tel = new NumeroDeTelephone();
        $tel->setIdContact($this->getObject());
        $telForm = new NumeroDeTelephoneForm($tel);
        $this->embedForm('Nouveau téléphone',$telForm);

        $this->embedRelation('NumerosDeFax');
        $fax = new NumeroDeFax();
        $fax->setIdContact($this->getObject());
        $faxForm = new NumeroDeFaxForm($fax);
        $this->embedForm('Nouveau fax', $faxForm);

        $this->embedRelation('Emails');
        $mail = new EMail();
        $mail->setIdContact($this->getObject());
        $mailForm = new EMailForm($mail);
        $this->embedForm('Nouveau mail', $mailForm);

        $this->embedRelation('NumerosDeCompteBancaire');
        $compte = new NumeroDeCompte();
        $compte->setIdContact($this->getObject());
        $compteForm = new NumeroDeCompteForm($compte);
        $this->embedForm('Nouveau compte bancaire', $compteForm);


        $this->validatorSchema['nom'] = new sfValidatorBlacklist(array('forbidden_values'=>array('toto','Manperdu','manperdu')));
    }


    // surchage pour permettre d'ignorer les formulaires vides et de mettre l'id généré quand le contact est nouveau
    public function saveEmbeddedForms($con = null, $forms = null) {

        if (null === $forms) {
            $tel = $this->getValue('Nouveau téléphone');
            if($tel['valeur'] == null) {
                unset($this->embeddedForms['Nouveau téléphone']);
            }else{
                $this->embeddedForms['Nouveau téléphone']->getObject()->setIdContact($this->getObject()->getId());
            }

            $fax = $this->getValue('Nouveau fax');
            if($fax['valeur'] == null) {
                unset($this->embeddedForms['Nouveau fax']);
            }else{
                $this->embeddedForms['Nouveau fax']->getObject()->setIdContact($this->getObject()->getId());
            }

            $mail = $this->getValue('Nouveau mail');
            if($mail['valeur'] == null) {
                unset($this->embeddedForms['Nouveau mail']);
            }else{
                $this->embeddedForms['Nouveau mail']->getObject()->setIdContact($this->getObject()->getId());
            }

            $compte = $this->getValue('Nouveau compte bancaire');
            if($compte['valeur'] == null) {
                unset($this->embeddedForms['Nouveau compte bancaire']);
            }else{
                $this->embeddedForms['Nouveau compte bancaire']->getObject()->setIdContact($this->getObject()->getId());
            }
        }

        return parent::saveEmbeddedForms($con, $forms);
    }

}
